package com.zhouzi.zhaoyang.ui.activity

import android.content.Intent
import android.os.Build
import android.view.KeyEvent
import android.widget.Toast
import com.afollestad.materialdialogs.MaterialDialog
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.zhouzi.zhaoyang.R
import com.zhouzi.zhaoyang.base.BaseActivity
import com.zhouzi.zhaoyang.bean.CloseAccountBean
import com.zhouzi.zhaoyang.bean.LoginBean
import com.zhouzi.zhaoyang.mvp.p.SettingActivityPresenter
import com.zhouzi.zhaoyang.mvp.v.ISettingActivityView
import com.zhouzi.zhaoyang.util.SPUtils
import com.zhouzi.zhaoyang.util.print.PrinterUtil
import kotlinx.android.synthetic.main.settings_activity.*

class SettingsActivity : BaseActivity(), ISettingActivityView {
    override fun errorToken() {
        exitToLogin()
    }

    var p = SettingActivityPresenter()

    init {
        p.attachView(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        p.detachView()
    }

    override fun getCloseAccountSuccess(data: CloseAccountBean?) {

        val util = PrinterUtil(this)

        var line = ArrayList<String>()
        line.add("当班汇总")
        line.add("门店名称：" + data?.storeName)
        line.add("销售数量：" + data?.sumCount)
        line.add("销售金额：" + data?.sumFee)
        line.add("微信支付数量：" + data?.wxCount)
        line.add("微信支付金额：" + data?.wxFee)
        line.add("支付宝支付数量：" + data?.aliCount)
        line.add("支付宝支付金额：" + data?.aliFee)
        line.add("会员支付数量：" + data?.memberCount)
        line.add("会员支付金额：" + data?.memberFee)
        line.add("退款支付数量：" + data?.refundCount)
        line.add("退款总金额：" + data?.refundFee)
        line.add("结算店员：" + data?.storeUserName)
        line.add("开始时间：" + data?.beginTime)
        line.add("结束时间：" + data?.endTime + "\n\n\n\n\n")


        util.createData(line)

        var activity = Intent(this, LoginActivity::class.java)
        startActivity(activity)
    }

    override fun getCloseAccountFailed(msg: String) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT)
    }

    override fun showLoadDialog() {
        loadDialog.show()
    }

    override fun dismissLoadDialog() {
        loadDialog.dismiss()
    }

    override fun getLayoutId(): Int {
        return R.layout.settings_activity
    }

    override fun initView() {

        var data = intent.getParcelableExtra<LoginBean>("personInfo")

        if (data != null) {

            Glide.with(this)
                .setDefaultRequestOptions(RequestOptions().error(R.mipmap.ic_logo))
                .load(data.logoUrl)
                .into(ivBrand)
            tvStoreName.setText("店铺名：" + data.storeName)
            tvUserName.setText("当前用户：" + data.username)

        }

        viewClick()



        var moneyMode = SPUtils.getInstance().getValue("moneyMode", -1)
        if (moneyMode == 1) {
            tvMoneyModeShow.setText("二维码")
        } else if (moneyMode == 2) {
            tvMoneyModeShow.setText("条码")

        }

    }

    var deviceNo = Build.SERIAL
    private fun viewClick() {


        tvQuery.setOnClickListener {
            var activity = Intent(this, TransactionQueryActivity::class.java)
            startActivity(activity)
        }

        tvModifyPW.setOnClickListener {
            var activity = Intent(this, ModifyPWActivity::class.java)
            startActivity(activity)
        }


        tvApBtn.setOnClickListener { finish() }


        ll_settings.setOnClickListener {

            val printSettings = Intent(this, PrintSettingsActivity::class.java)
            startActivity(printSettings)

        }

        cbVip.setOnClickListener {
            SPUtils.getInstance().putValue("vip", cbVip.isChecked)
        }

        cbSoftKeyboard.setOnClickListener {
            SPUtils.getInstance().putValue("SoftKeyboard", cbSoftKeyboard.isChecked)
        }



        llCloseAccount.setOnClickListener {

            var closeAccount = Intent(this, CloseAccountActivity::class.java)
            startActivity(closeAccount)

        }


        tvMoneyMode.setOnClickListener {

            MaterialDialog.Builder(this)
                .title("收款模式")
                .positiveText("二维码")
                .negativeText("条码")
                .onPositive { dialog, which ->

                    SPUtils.getInstance().putValue("moneyMode", 1)
                    tvMoneyModeShow.setText("二维码")

                }
                .onNegative { dialog, which ->

                    SPUtils.getInstance().putValue("moneyMode", 2)
                    tvMoneyModeShow.setText("条码")

                }
                .build()
                .show()

        }


    }


    override fun onStart() {
        super.onStart()

        var vip = SPUtils.getInstance().getValue("vip", true) as Boolean
        cbVip.isChecked = vip

        var softKeyboard = SPUtils.getInstance().getValue("SoftKeyboard", true) as Boolean
        cbSoftKeyboard.isChecked = softKeyboard


    }


    var back = 67
    override fun dispatchKeyEvent(event: KeyEvent?): Boolean {

        if (event?.keyCode == back) {
            finish()
        }
        return super.dispatchKeyEvent(event)
    }


}
